import request from './request'

// 登录请求参数
export interface LoginRequest {
  username: string
  password: string
}

// 注册请求参数
export interface RegisterRequest {
  username: string
  email: string
  nickname: string
  password: string
  confirmPassword?: string
}

// 用户信息接口
export interface UserInfo {
  userId: number
  username: string
  email: string
  nickname?: string
  phone?: string
  avatarUrl?: string
  birthDate?: string
  gender?: number
  bio?: string
  status: number
  lastLoginAt?: string
  createdAt: string
}

// 登录响应
export interface LoginResponse {
  token: string
  user: UserInfo
}

// API响应格式 (对应后端AjaxResult)
export interface ApiResponse<T = any> {
  code: number
  msg: string
  data?: T
}

/**
 * 用户登录
 */
export function login(data: LoginRequest) {
  return request.post<ApiResponse<LoginResponse>>('/auth/login', data)
}

/**
 * 用户注册
 */
export function register(data: RegisterRequest) {
  return request.post<ApiResponse<string>>('/auth/register', data)
}

/**
 * 用户登出
 */
export function logout() {
  return request.post<ApiResponse<string>>('/auth/logout')
}

/**
 * 刷新token
 */
export function refreshToken(refreshToken: string) {
  return request.post<ApiResponse<LoginResponse>>('/auth/refresh', null, {
    params: { refreshToken }
  })
}

/**
 * 获取当前用户信息
 */
export function getCurrentUser() {
  return request.get<ApiResponse<UserInfo>>('/auth/info')
}

/**
 * 更新用户信息
 */
export function updateUserInfo(data: Partial<UserInfo>) {
  return request.put<ApiResponse<UserInfo>>('/auth/info', data)
}

/**
 * 修改密码
 */
export function changePassword(data: { oldPassword: string; newPassword: string }) {
  return request.post<ApiResponse<string>>('/auth/change-password', data)
}

/**
 * 忘记密码 - 发送重置邮件
 */
export function forgotPassword(email: string) {
  return request.post<ApiResponse<string>>('/auth/forgot-password', { email })
}

/**
 * 重置密码
 */
export function resetPassword(data: { token: string; newPassword: string }) {
  return request.post<ApiResponse<string>>('/auth/reset-password', data)
}